<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
import { patientDeleteAPI, patientListAPI } from '@/api/patient';
import { useConsultStore } from '@/store/Consult';
import type { PatientItem } from '@/types/patient';
import { onReady, onShow } from '@dcloudio/uni-app';
import { ref } from 'vue'

const swipeOptions = ref([
  {
    text: '删除',
    style: {
      backgroundColor: '#dd524d',
      color: '',
      fontSize: '',
    },
  },
])

const PatientList = ref<PatientItem[]>([])
const GetpatientList = async () => {
  const res = await patientListAPI()
  PatientList.value = res.data
}
onShow(() => {
  GetpatientList()
})

// 删除患者
const onSwipeActionClick = async (id: string, index: number) => {
  // 前端删除
  PatientList.value.splice(index, 1)
  // 后端删除
  await patientDeleteAPI(id)
  // 提示-确认弹窗
  uni.showToast({ title: '删除成功', icon: 'success' })
}

// 获取页面参数
const props = defineProps<{
  type:'consult'
}>()
console.log(props);


onReady(() => {
  uni.setNavigationBarTitle({
    title: props.type === 'consult' ? '选择患者' : '患者列表'
  })
})

// 选择患者
const store = useConsultStore()
const handleSelectPatient = (id: string) => { 
  // 如果不是选择患者,则继续操作
  if (props.type !== 'consult' )
    return 
  // 保存患者id
  store.patientId = id
  console.log(store.patientId);
}

const handleNextStep = () => {
  uni.navigateTo({
    url: '/subpkg_consult/pages/payment/payment'
  })
}
</script>
<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item :right-options="swipeOptions" v-for="(item,index) in PatientList" :key="item.id" @click="onSwipeActionClick(item.id,index)">
          <view class="archive-card" 
          :class="{
            active: props.type === 'consult' ? item.id === store.patientId : item.defaultFlag === 1,
            
          }"
          >
            <view class="archive-info" @click="handleSelectPatient(item.id)">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{ item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2') }}</text>
              <text class="default" v-if="item.defaultFlag === 1">默认</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.genderValue }}</text>
              <text class="age">{{item.age}}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" :url="`/subpkg_archive/pages/from/from?id=${item.id}`">
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item>
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="PatientList.length < 6" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/pages/from/from">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <view class="next-step" v-if="props.type === 'consult'">
      <button class="uni-button" :disabled="store.patientId === ''" @click="handleNextStep">下一步</button>
    </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>